var QcReportSearchModel = function () {
  BaseModel.call(this);

  var self = this;
  var propertyList = ['orderId', 'companyOrderId', 'factoryId', 'customerId', 'deptId', 'workShopId', 'processId', 'createUserId', 'selfLackStart',
    'selfLackEnd', 'siteLackStart', 'siteLackEnd'
  ];
  var baseFields = ['orderId', 'companyOrderId', 'factoryId', 'customerId', 'deptId', 'workShopId', 'processId', 'createUserId'];
  var fn = {
    initData: function () {
      var fields = baseFields;
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        self[field] = '';
      }

      var numFields = ['selfLackStart', 'selfLackEnd', 'siteLackStart', 'siteLackEnd'];
      for (var i = 0; i < numFields.length; i++) {
        var field = numFields[i];
        self[field] = undefined;
      }
      self.saveOldData();
    }
  };

  this.saveOldData = function () {
    this.setOldData(propertyList);
  };

  this.resetOldData = function () {
    for (var i = 0; i < propertyList.length; i++) {
      var property = propertyList[i];
      var value = this.getOldData(property);
      value = _.cloneDeep(value);
      this[property] = value;
    }
  };

  this.isDataChange = function () {
    var fields = baseFields;
    return this.isChange(fields);
  };

  this.clearData = function () {
    fn.initData();
  };

  this.resetSearch = function(dropdownData){
    var fields = baseFields;
    var filterObj = {};
    for (var i = 0; i < fields.length; i++) {
      var field = fields[i];
      var value = this[field];
      if (SysUtil.isEmpty(value)) {
        continue;
      }
      filterObj[field] = value;
    }

    if (SysUtil.isEmptyObjct(filterObj)) {
      return;
    }
    var obj = _.find(dropdownData, filterObj);
    if (obj === undefined) {
      this.clearData();
    }
  };

  this.getObject = function () {
    var postData = {};
    this.setPostData(postData, propertyList);
    return postData;
  };

  fn.initData();
};

var QcReportSearchForm = function () {
  var dropdownDictionary = {
    orderId: "系统编号",
    companyOrderId: "订单编号",
    factoryId: "工厂型号",
    customerId: "客户型号",
    deptId: "部门",
    workShopId: "车间",
    processId: "工序",
    createUserId: "姓名"
  };

  var template = /* html */ `
    <div class="qcreport-search-form">
      <el-form inline label-width="auto">
        <template v-for="searchField in searchFields">
          <el-form-item :key="searchField" :label="dropdownDictionary[searchField]">
            <page-select clearable filterable has-null-option class="search-input" v-model="search[searchField]" :options="getSelectOptions(searchField)"
              :placeholder="'请选择'+dropdownDictionary[searchField]" @visible-change="function(visible){visibleChange(searchField,visible)}" 
              @change="searchChange(searchField)"></page-select>
          </el-form-item>
        </template>

        <el-form-item label="自检空缺次数" label-width="100px">
          <el-input-number style="width:195px" v-model="search.selfLackStart" :controls="false" :min="0" :max="search.selfLackEnd" :precision="0"
            placeholder="请输入自检空缺最小数"></el-input-number>
          <span>-</span>
          <el-input-number style="width:195px" v-model="search.selfLackEnd" :controls="false" :min="search.selfLackStart?search.selfLackStart:0" :precision="0"
            placeholder="请输入自检空缺最大数"></el-input-number>
        </el-form-item>
        <el-form-item label="巡检空缺次数" label-width="100px">
          <el-input-number style="width:195px" v-model="search.siteLackStart" :controls="false" :min="0" :max="search.siteLackEnd" :precision="0"
            placeholder="请输入巡检空缺最小数"></el-input-number>
          <span>-</span>
          <el-input-number style="width:195px" v-model="search.siteLackEnd" :controls="false" :min="search.siteLackStart?search.selfLackStart:0" :precision="0"
            placeholder="请输入巡检空缺最大数"></el-input-number>
        </el-form-item>
      </el-form>
      <div style="text-align:center">
        <el-button type="primary" @click="searchData">查询</el-button>
      </div>
    </div>
  `;

  return {
    props: {
      search: {
        type: Object,
        required: true
      },
      dropdownData: {
        type: Array,
        default: function () {
          return [];
        }
      },
      isShow: {
        type: Boolean,
        default: false
      }
    },

    watch: {
      dropdownData: function () {
        this.clearDropdownList();
      },

      isShow: function (val) {
        if (!val) {
          this.oldSearch = _.cloneDeep(this.search);
          return;
        }

        var search = this.search;
        var oldSearch = this.oldSearch;
        var fields = this.searchFields;
        var isChange = false;
        for (var i = 0; i < fields.length; i++) {
          var field = fields[i];
          if (!_.isEqual(search[field], oldSearch[field])) {
            isChange = true;
            break;
          }
        }
        if (isChange) {
          this.clearDropdownList();
        }
      }
    },

    data: function () {
      var fields = ['orderId', 'companyOrderId', 'factoryId', 'customerId', 'deptId', 'workShopId', 'processId', 'createUserId'];
      var vueData = {
        searchFields: fields,
        dropdownDictionary: dropdownDictionary,
        oldSearch: _.cloneDeep(this.search)
      };
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        vueData[`${field}List`] = undefined;
      }
      return vueData;
    },

    methods: {
      getSelectOptions: function (field) {
        var options = this[`${field}List`];
        if (options === undefined) {
          return [];
        }
        return options;
      },

      searchChange: function (field) {
        this.clearDropdownList(field);
      },

      clearDropdownList: function (field) {
        var searchFields = this.searchFields;
        for (var i = 0; i < searchFields.length; i++) {
          var searchField = searchFields[i];
          if (searchField === field) {
            continue;
          }
          this[`${searchField}List`] = undefined;
        }
      },

      visibleChange: function (field, visible) {
        if (visible) {
          this.filterSelectOptions(field);
        }
      },

      filterSelectOptions: function (field) {
        var dropList = this[`${field}List`];
        if (dropList !== undefined) {
          return;
        }

        var labelField = '';
        if (field === 'deptId') {
          labelField = 'deptName';
        } else if (field === 'workShopId') {
          labelField = 'workShopName';
        } else if (field === 'processId') {
          labelField = 'processName';
        } else if (field === 'createUserId') {
          labelField = 'createUser';
        }

        dropList = this.getDropdownList(field, labelField);
        this[`${field}List`] = dropList;
      },

      getDropdownList: function (field, labelField) {
        var search = this.search;
        var searchFields = this.searchFields;
        var filterObj = {};
        for (var i = 0; i < searchFields.length; i++) {
          var searchField = searchFields[i];
          if (searchField === field) {
            continue;
          }
          var value = search[searchField];
          if (!SysUtil.isEmpty(value)) {
            filterObj[searchField] = value;
          }
        }

        var dropdownData = this.dropdownData;
        if (!SysUtil.isEmptyObjct(filterObj)) {
          dropdownData = _.filter(dropdownData, filterObj);
        }
        var dropList = [];
        for (var i = 0; i < dropdownData.length; i++) {
          var data = dropdownData[i];
          var value = data[field];
          if (typeof value === 'string') {
            if (SysUtil.isEmpty(value.trim())) {
              continue;
            }
          } else {
            if (SysUtil.isEmpty(value)) {
              continue;
            }
          }

          if (labelField === '') {
            dropList.push(value);
            continue;
          }

          var labelValue = data[labelField];
          if (typeof labelValue === 'string') {
            labelValue = labelValue.trim();
          }
          dropList.push({
            value: value,
            label: labelValue
          });
        }
        if (labelField === '') {
          dropList = _.uniq(dropList);
        } else {
          dropList = _.uniqBy(dropList, field);
        }
        return dropList;
      },

      searchData: function () {
        this.$emit('search-data', this.search);
      }
    },

    components: {
      PageSelect: PageSelect
    },

    template: template
  };
}();
